<?php

/**
 * This is the model class for table "prd_product".
 *
 * @author huytbt
 * @date 2011-06-27
 * @version 1.0
 * 
 * The followings are the available columns in table 'prd_product':
 * @property string $id
 * @property string $module_id
 * @property string $name
 * @property string $categories
 * @property string $alias
 * @property string $image
 * @property string $catalog_image
 * @property string $description
 * @property string $content
 * @property integer $priority
 * @property string $created
 * @property string $modified
 * @property string $product_date
 * @property integer $is_published
 * @property integer $is_special
 * @property integer $price
 * @property integer $special_price
 * @property integer $no_in_stock
 */
class Products extends CActiveRecord
{
	const PUBLISHED = 1;
	const UNPUBLISHED = 0;
	public $n_date;
	public $n_hours;
	public $n_minutes;
	public $enableLanguage = true;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Products the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_products';
	}
	/**
	 * @return string the primary key of the associated database table.
	 */
	public function primaryKey()
	{
		return 'id';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, alias, description, price', 'required'),
			array('id, alias', 'unique'),
			array('priority, is_published,is_special', 'numerical', 'integerOnly' => true),
			array('n_hours, n_minutes, quantity', 'numerical', 'integerOnly' => true, 'min' => 0, 'max' => 59),
			array('name', 'length', 'max' => 128),
			array('alias', 'length', 'max' => 64),
			array('image, catalog_image', 'file', 'types' => 'jpg, gif, png, jpeg', 'allowEmpty' => true),
			array('image, catalog_image', 'unsafe'),
			array('special_price','length','max'=>15),
			array('price','length','max'=>15),
			array('description, content, created, modified, product_date, n_date, n_hours, n_minutes, style', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, alias, image, catalog_image, description, content, priority, created, modified, product_date, is_published, quantity', 'safe', 'on' => 'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
    	//Yii::import('application.modules.products.models.ProductCategories');
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
       		'categories'=>array(self::MANY_MANY, 'ProductCategories','product_middle(product_id, category_id)'),
			'images' => array(self::HAS_MANY, 'ProductImage', 'product_id'),
			'mapcat' => array(self::HAS_MANY, 'ProductMiddle', 'product_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'alias' => 'Alias',
			'categories' => 'Categories',
			'image' => 'Image',
			'catalog_image' => 'Catalog Image',
			'description' => 'Description',
			'content' => 'Content',
			'priority' => 'Priority',
			'created' => 'Created',
			'modified' => 'Modified',
			'product_date' => 'Product Date',
			'is_published' => 'Is Published',
			'n_hours' => 'Hours',
			'n_monutes' => 'Minutes',
			'is_special'=>'Is Special',
			'price'=>'Price',
			'special_price'=>'Special Price',
			'quantity'=>'Quantity in stock',
			'style'=>'Style',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id, true);
		$criteria->compare('name', $this->name, true);
		$criteria->compare('alias', $this->alias, true);
		$criteria->compare('image', $this->image, true);
		$criteria->compare('catalog_image', $this->catalog_image, true);
		$criteria->compare('description', $this->description, true);
		$criteria->compare('content', $this->content, true);
		$criteria->compare('priority', $this->priority);
		$criteria->compare('created', $this->created, true);
		$criteria->compare('modified', $this->modified, true);
		$criteria->compare('product_date', $this->product_date, true);
		$criteria->compare('is_published', $this->is_published);
		$criteria->compare('is_special', $this->is_special);
		$criteria->compare('price', $this->price);
		$criteria->compare('special_price', $this->special_price);
		$criteria->compare('quantity', $this->quantity);
		$criteria->compare('style', $this->style);
		$criteria->order = 'created desc';
		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
			'pagination' => array(
				'pageSize' => Yii::app()->getModule('products')->entriesShowInEditPage,
			),
		));
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function category_search($category = null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;
	
		$criteria->together = true;
		$criteria->with=array('categories');
		$criteria->compare('t.id', $this->id, true);
		$criteria->compare('t.name', $this->name, true);
		$criteria->compare('t.alias', $this->alias, true);
		$criteria->compare('t.image', $this->image, true);
		$criteria->compare('t.catalog_image', $this->catalog_image, true);
		$criteria->compare('t.description', $this->description, true);
		$criteria->compare('t.content', $this->content, true);
		$criteria->compare('t.priority', $this->priority);
		$criteria->compare('t.created', $this->created, true);
		$criteria->compare('t.modified', $this->modified, true);
		$criteria->compare('t.product_date', $this->product_date, true);
		$criteria->compare('t.is_published', $this->is_published);
		$criteria->compare('t.is_special', $this->is_special);
		$criteria->compare('t.categories.name', ProductCategories::model()->name);
		$criteria->compare('t.categories.alias', ProductCategories::model()->alias);
		$criteria->compare('price', $this->price);
		$criteria->compare('special_price', $this->special_price);
		$criteria->compare('quantity', $this->quantity);
		$criteria->compare('style', $this->style);
		$criteria->order = 't.created desc';
		$criteria->addCondition('"categories"."alias" = :alias');
		$criteria->params = array(':alias' => $category);
		//$criteria->condition = "'categories.alias' = '". $category ."'";
		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
			'pagination' => array(
				'pageSize' => Yii::app()->getModule('products')->entriesShowInEditPage,
			),
		));
	}
	
	public function behaviors() {
	    return array(
	        'AutoTimestampBehavior' => array(
	            'class' => 'application.components.AutoTimestampBehavior',
	            //You can optionally set the field name options here
	        )
	    );
	}
	
	public static function webSearch($keyword){
		$criteria = new CDbCriteria();
		$criteria->select = 'id,name,alias,image,description,created';
		$criteria->addSearchCondition('name', $keyword,true,'OR');
		$criteria->addSearchCondition('content', $keyword,true,'OR');
		$criteria->order = 'created DESC';
		return self::model()->findAll($criteria);
	}
	public function getRelatedProduct($product_id = NULL, $arrayCat = array(), $offset = 0, $limit = 9) {
		$model =array();
		
		if (isset($arrayCat) && !empty($arrayCat)) {
			$criteria = new CDbCriteria();
			// $criteria->condition = "category_id = :CategoryId AND t.product_id <> :ProductId";
			$criteria->condition = "t.product_id <> :ProductId";
			// $criteria->params = array(':CategoryId'=>$catId, ':ProductId' => $product_id);
			$criteria->params = array( ':ProductId' => $product_id);
			$criteria->addInCondition('category_id',$arrayCat); 
			$criteria->with = 'products';
			$criteria->offset = $offset;
			$criteria->limit = $limit;
			$criteria->group = 'products.id';
			$criteria->order = "products.modified DESC, products.created DESC";
			// dump($criteria);
			$model = ProductMiddle::model()->findAll($criteria);
		}
			
		return !empty($model) ? $model : array();
	}
	
	public function getFeaturedProducts($offset = 0, $limit = 2) {
		$criteria = new CDbCriteria();
		$criteria->offset = $offset;
		$criteria->limit = $limit;
		$criteria->order = "is_published ASC, created DESC, modified DESC";
		
		$model = Products::model()->findAll($criteria);
		
		return !empty($model) ? $model : array();
	}
}